package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.LabelValue;
import cn.edu.sdyu.ie.crs.common.response.NamedValue;
import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.domain.Classroom;
import cn.edu.sdyu.ie.crs.domain.ClassroomEnvironment;
import cn.edu.sdyu.ie.crs.service.ClassroomEnvironmentService;
import cn.edu.sdyu.ie.crs.service.ClassroomService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.Parameter;
//import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "教室管理", description = "教室信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/classroom")
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ClassroomEnvironmentService classroomEnvironmentService;

    /**
     * 添加新教室信息
     *
     * @param classroom
     * @return
     */
    @Operation(tags = "教室管理", summary = "添加教室信息")
    @PostMapping("")
    public Result<Void> addClassroom(Classroom classroom) {

        if (classroom.getId() != null && !classroom.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。");
            return Result.failure();
        }
        classroomService.saveClassroom(classroom);
        return Result.success();
    }

    /**
     * 更新教室信息
     *
     * @param classroom
     * @return
     */
    @Operation(tags = "教室管理", summary = "修改教室信息")
    @PutMapping("")
    public Result<Void> updateClassroom(Classroom classroom) {

        if (classroom.getId() == null || classroom.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        classroomService.saveClassroom(classroom);
        return Result.success();
    }

    /**
     * 删除指定ID的教室信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "教室管理", summary = "删除教室信息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteClassroomById(@PathVariable Integer id) {
        classroomService.deleteClassroomById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的教室信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "教室管理", summary = "查询指定ID的教室信息")
//    @Parameters(value = {
//            @Parameter(description = "教室ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教室ID")
    })
    @GetMapping("/{id}")
    public Result<Classroom> findClassroomById(/*@Parameter(description = "教室ID")*/ @PathVariable Integer id) {
        Classroom classroom = classroomService.findClassroomById(id);

        if (classroom == null) {
            return Result.failure();
        }

        return Result.success(classroom);
    }

//    @Operation(tags = "教室管理", summary = "查询全部教室信息")
//    @GetMapping("/")
//    public Result<List<Classroom>> findAll() {
//        List<Classroom> list = classroomService.findAll();
//        return Result.success(list);
//  }

    /**
     * 分页查询教室信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "教室管理", summary = "分页查询教室信息")
    @GetMapping("")
    public Result<PageVO<Classroom>> listClassroomsPage(@RequestParam(defaultValue = "1") Integer page,
                                                      @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Classroom> list = classroomService.findByPage(page - 1, pageSize);
        PageVO<Classroom> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }

    /**
     * 统计总教室数
     */
    @Operation(tags = "教室管理", summary = "统计总教室数")
    @GetMapping("count/all")
    public Result<Long> countClassroom() {
        Long count = classroomService.countClassroom();
        return Result.success(count);
    }

    /**
     * 根据使用状态统计教室数
     * @param usageStatus
     * @return
     */
    @Operation(tags = "教室管理", summary = "根据使用状态统计教室数")
    @GetMapping("count/usageStatus")
    public Result<Long> countClassroomByUsageStatus(@RequestParam Integer usageStatus) {
        Long count = classroomService.countClassroomByUsageStatus(usageStatus);
        return Result.success(count);
    }

    /**
     * 按照使用模式分组统计教室数
     * @return
     */
    @Operation(tags = "教室管理", summary = "按照使用模式分组统计教室数")
    @GetMapping("count/groupByUsageMode")
    public Result<List<NamedValue>> countGroupByUsageMode() {
        List<NamedValue> count = classroomService.countGroupByUsageMode();
        return Result.success(count);
    }

    /**
     * 根据教学楼ID列出教室
     * @param classroomBuildingId
     * @return
     */
    @Operation(tags = "教室管理", summary = "根据教学楼ID列出教室")
    @GetMapping("/classroomBuilding")
    public Result<List<Classroom>> findClassroomByLocation(Integer classroomBuildingId) {
        List<Classroom> list = classroomService.findClassroomByClassroomBuildingId(classroomBuildingId);
        return Result.success(list);
    }

    @GetMapping("/{id}/environment")
    public Result<ClassroomEnvironment> findClassroomEnvironmentByClassroomId(@PathVariable Integer id) {
        ClassroomEnvironment classroomEnvironment = classroomEnvironmentService.findClassroomEnvironmentByClassroomId(id);
        return Result.success(classroomEnvironment);
    }
}
